package youcaihuaoa.weetop.com.vm.main

import android.app.Application
import androidx.databinding.ObservableField
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.BannerBean
import com.robot.base.model.ExamineBean
import com.robot.base.util.LiveDataBus
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import youcaihuaoa.weetop.com.vm.apply.MyApplyListViewModel
import youcaihuaoa.weetop.com.vm.apply.MyExamineListViewModel

class HomeFragmentViewModel(application: Application) : GLBaseViewModel(application) {

    val isMyApplyTab = ObservableField<Boolean>(true)

    val clickMyApplyTabCommand = BindingCommand<Any>(BindingAction {
        isMyApplyTab.set(true)
        examineUserData()
    })

    val clickMessageCommand = BindingCommand<Any>(BindingAction {
        LiveDataBus.get().with(AppConstants.LiveKey.TO_MAIN_TAB,Int::class.java).postValue(1)
    })

    val clickWaitApplyTabCommand = BindingCommand<Any>(BindingAction {
        isMyApplyTab.set(false)
        examineUserData()
    })

    val toMyApplyCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_MY_APPLY)
    })
    val toSignCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_SIGN)
    })

    val clickJumpMyApplyListCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_MY_EXAMINE_LIST)
    })
    var uiEvent = UIEvent()

    class UIEvent {
        val bannerDataEvent = SingleLiveEvent<List<BannerBean>>()
        val dataEvent = SingleLiveEvent<List<ExamineBean>>()
    }

    fun fetchBanner() {
        UserServiceFactory.banner()
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<BannerBean>>>(this) {
                override fun onNext(t: BaseResponse<List<BannerBean>>) {
                    uiEvent.bannerDataEvent.postValue(t.data)
                }
            })
    }

    fun examineUserData() {
        if(isMyApplyTab.get() == true){
            UserServiceFactory.applyUserData(
                "2"
            )
        }else{
            UserServiceFactory.examineUserData(
                "1"
            )
        }
        .subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<ExamineBean>>>(this) {
            override fun onNext(t: BaseResponse<List<ExamineBean>>) {
                uiEvent.dataEvent.postValue(t.data)
            }
        })
    }
}